package com.xja.shoppingmall.controller;

import com.xja.shoppingmall.util.TokenParseUtil;
import com.xja.shoppingmall.vo.LoginUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author CHENCHEN
 * @title  Nacos服务发现
 * @createTime 2021-12-1021:54
 */
@Slf4j
@RequestMapping("nacos-client")
@RestController
public class NacosClientController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("")
    public List<ServiceInstance> logNacosClientInfo(@RequestParam(defaultValue = "SHOPPINGMALL-NACOS-CLIENT") String serviceId){
        log.info("coming in log nacos client info:[{}]", serviceId);
        return discoveryClient.getInstances(serviceId);
    }


    @RequestMapping("getToken")
    public LoginUserInfo getToken(@RequestHeader("authority") String authority) throws Exception {
        log.info("get authority value is : [{}]", authority);
        return TokenParseUtil.parseUserInfoFromToken(authority);
    }
}
